package com.java.util;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class UrlUtil {
    public static String encodeParams(Map<String, List<String>> maps) {
        if (null == maps || maps.size() == 0) {
            return null;
        } else {
            StringBuffer buffer = new StringBuffer(150);
            int i = 0;
            for (Map.Entry<String, List<String>> set : maps.entrySet()) {
                String key = set.getKey();
                List<String> values = set.getValue();
                if (i == 0) {
                    i++;
                } else {
                    buffer.append("&");
                }

                buffer.append(key).append("=");
                if (null != values && values.size() > 0) {
                    String value = values.get(0);
                    if (StringUtils.isNotEmpty(value)) {
                        buffer.append(value);
                    }
                }
            }
            return buffer.toString();
        }
    }

    public static Map<String, String> decodeParams(String s) {
        return decodeParams(s, Charset.forName("utf-8"));
    }
    
    public static Map<String, String> decodeParams(String s, Charset charset) {
        Map<String, String> params = new LinkedHashMap();
        String name = null;
        int pos = 0;
        int i;
        char c = 0;
        for (i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (c == '=' && name == null) {
                if (pos != i) {
                    name = decodeComponent(s.substring(pos, i), charset);
                }
                pos = i + 1;
            } else if (c == '&') {
                if (name == null && pos != i) {
                    addParam(params, decodeComponent(s.substring(pos, i), charset), "");
                } else if (name != null) {
                    addParam(params, name, decodeComponent(s.substring(pos, i), charset));
                    name = null;
                }
                pos = i + 1;
            }
        }

        if (pos != i) {
            if (name == null) {
                addParam(params, decodeComponent(s.substring(pos, i), charset), "");
            } else {
                addParam(params, name, decodeComponent(s.substring(pos, i), charset));
            }
        } else if (name != null) {
            addParam(params, name, "");
        }

        return params;
    }

    private static String decodeComponent(String s, Charset charset) {
        if (s == null) {
            return "";
        }

        try {
            return URLDecoder.decode(s, charset.name());
        } catch (UnsupportedEncodingException e) {
            throw new UnsupportedCharsetException(charset.name());
        }
    }
    private static void addParam(Map<String, String> params, String name, String value) {
    	
        String values = params.get(name);
        if (values == null) {
            params.put(name, value);
        }
    }
    
    public static String getRemoteHost(HttpServletRequest  request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
       /* if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }*/
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }
}
